package net.lz1998.pbbot.plugin;

import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.constant.GroupInfoConstant;
import net.lz1998.pbbot.utils.Msg;
import onebot.OnebotApi;
import onebot.OnebotEvent;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 成员信息功能
 */
@Component
public class GetFGroupInfoPlugin  extends BotPlugin {

    @Override
    public int onGroupMessage(@NotNull Bot bot, @NotNull OnebotEvent.GroupMessageEvent event) {
        String info="";
        String msg = event.getRawMessage().trim();
        Long groupId = event.getGroupId();
        Long adminId = event.getUserId();
        String[] arr = msg.split(" ");
        OnebotApi.GetGroupMemberInfoResp groupMemberInfo = null;
        OnebotApi.GetStrangerInfoResp strangerInfo = null;
        if(msg.startsWith(GroupInfoConstant.HEARD)){
            if(arr.length==1){
                groupMemberInfo = bot.getGroupMemberInfo(groupId, adminId, true);
                strangerInfo = bot.getStrangerInfo(adminId);
            }else if((arr.length==3)){
                if (GroupInfoConstant.GROUPID == groupId ) {
                    if (arr[2].startsWith("qq=")) {
                        String[] idArr = arr[2].split("=");
                        Long user_id = Long.valueOf(idArr[1].substring(1, idArr[1].lastIndexOf('"')));
                        groupMemberInfo = bot.getGroupMemberInfo(groupId, user_id, true);
                        strangerInfo = bot.getStrangerInfo(user_id);
                    } else {
                        bot.sendGroupMsg(groupId, Msg.builder().at(adminId).text(GroupInfoConstant.INFO), true);
                        return MESSAGE_IGNORE;
                    }
                }
            }else{
                bot.sendGroupMsg(groupId, Msg.builder().at(adminId).text(GroupInfoConstant.INFO), true);
                return MESSAGE_IGNORE;
            }
        }

        if(null!=groupMemberInfo){

            String sex=strangerInfo.getSex().equals("male")?GroupInfoConstant.SEX2:GroupInfoConstant.SEX1;

            info+="昵称\t\t"+groupMemberInfo.getNickname()+"\n";
            info+="年龄\t\t"+strangerInfo.getAge()+"\n";
            info+="性别\t\t"+sex+"\n";
            info+="地址\t\t"+groupMemberInfo.getArea()+"\n";
            info+="等级\t\t"+groupMemberInfo.getLevel()+"\n";
            info+="头衔\t\t"+groupMemberInfo.getTitle()+"\n";
            info+="加群时间\t\t"+timestamp2Date((long) groupMemberInfo.getJoinTime())+"\n";
            info+="最后一次发言时间\t\t"+timestamp2Date((long) groupMemberInfo.getLastSentTime())+"\n";
            info+="\n";
            info+=sex.equals(GroupInfoConstant.SEX1)?info=GroupInfoConstant.HANZI:GroupInfoConstant.MEIZI;
            bot.sendGroupMsg (groupId, info, true);
        }

/*
        Msg msg1 = Msg.builder()
                .text(info)
                .image("http://47.93.130.192:8088/mirai/1.jpg");
*/
        return MESSAGE_IGNORE;
    }


    public static String timestamp2Date(Long time){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time * 1000));
    }
}




